﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Cwt;

namespace WebApp._Management._PartnerLinks
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string act = Request.QueryString["act"];
                if (act == "delete")
                {
                    Guid id = new Guid(Request.QueryString["id"]);
                    ICwtService service = Global.Get<ICwtService>();
                    PartnerLink link = service.FindPartnerLink(id);
                    if (link != null)
                    {
                        if (service.DeletePartnerLink(link))
                        {
                            Response.Redirect("./list.aspx");
                            return;
                        }
                    }
                    Response.Redirect("../Message.aspx");
                }
                else
                {
                    ICwtService service = Global.Get<ICwtService>();
                    _ctlRepeater.DataSource = service.FindPartnerLinks();
                    _ctlRepeater.DataBind();
                }
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            string caption = _ctlEditCaption.Text;
            string imageUrl = _ctlEditImageUrl.Text;
            string siteUrl = _ctlEditSiteUrl.Text;
            int order = Convert.ToInt32(_ctlEditOrder.Text);

            PartnerLink link = new PartnerLink();
            link.Id = Guid.NewGuid();
            link.Caption = caption;
            link.ImageUrl = imageUrl;
            link.SiteUrl = siteUrl;
            link.Order = order;
            ICwtService service = Global.Get<ICwtService>();

            if (service.SavePartnerLink(link))
            {
                Response.Redirect("./list.aspx");
            }
        }
    }
}